<template>
  <Dialog v-model="show" @confirm="handleSubmit" @close="handleClose" title="新增管理员" width="500">
    <el-form :model="formData" label-width="100px" ref="formRef" :rules="rules">
      <el-form-item label="权限组名称" prop="name">
        <el-input v-model="formData.name" maxlength="20" show-word-limit />
      </el-form-item>
      <el-form-item label="权限组描述" prop="description">
        <el-input v-model="formData.description" type="textarea" rows="4" show-word-limit maxlength="50" />
      </el-form-item>
    </el-form>
  </Dialog>
</template>
<script setup>
import { ref, watch } from "vue";
import Dialog from "@/components/dialog.vue";

const show = defineModel();
const props = defineProps({
  data: {
    type: Object,
    default: () => ({}),
  },
});
watch(show, (val) => {
  if (val) {
    props.data && (formData.value = props.data);
  }
});
const formData = ref({
  name: "",
  description: "",
});
const rules = ref({
  name: [{ required: true, message: "请输入权限组名称", trigger: "blur" }],
  description: [{ required: true, message: "请输入权限组描述", trigger: "blur" }],
});
const formRef = ref();
const handleSubmit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      console.log("submit!");
    } else {
      console.log("error submit!");
    }
  });
};
const handleClose = () => {
  formData.value.name = "";
  formData.value.description = "";
  show.value = false;
};
</script>
